//
//  StudyLoginVC.swift
//  NewXueBa
//
//  Created by Shine on 2019/6/3.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import UIKit

class StudyLoginVC: UIViewController {

    @IBOutlet weak var phoneTextF: UITextField!
    @IBOutlet weak var pswTextF: UITextField!
    @IBOutlet weak var pswEyeBtn: UIButton!
    @IBOutlet weak var loginBtn: BCUIButton!
    @IBOutlet weak var eyeBtn: UIButton!
    
    let dispose = DisposeBag()
    let vm = LoginVM()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 初始化眼睛
        eyeBtn.isSelected = false
        setLoginUI()
        obserState()
    }
    
    fileprivate func setLoginUI() {
        phoneTextF.text = BCUserManager.shareManager.user_phone
        pswTextF.text = BCUserManager.shareManager.user_inputPwd
    }
    
    fileprivate func obserState() {
        //账号的观察转换 $0.validateText(.PhoneNumber)
        let accountState = phoneTextF.rx.text.orEmpty
                           .map{ $0.count>0 }
                           .share(replay: 1, scope: .whileConnected)
        //密码的观察转换
        let pswState = pswTextF.rx.text.orEmpty
                       .map{ $0.count > 0 }
                       .share(replay: 1, scope: .whileConnected)
        //合成新的观察者
        let comState = Observable.combineLatest(accountState, pswState) { (state1, state2) -> Bool in
            state1 && state2
        }
        //绑定登录按钮
        comState.bind(to: loginBtn.ex_loginBtnState)
                .disposed(by: dispose)
        
    }
    
    @IBAction func loginBtnClick(_ sender: BCUIButton) {
        vm.loginRequest(phoneTextF.text!, pswTextF.text?.md5 ?? "", BC_Ver, BC_Ctype) {
            BCUserManager.shareManager.user_inputPwd = self.pswTextF.text
            UIApplication.shared.delegate?.window??.rootViewController = BCBaseTabBarVC.rootTabbarClass()
            DispatchQueue.global().async {
                //耗时操作
            WebSocketManager.sharedInstance().loginWebSocketBaseUrl(XueBa_WebSocketURL, port: XueBa_WebSocketPort, userUid: BCUserManager.shareManager.user_uid ?? "", userToken: BCUserManager.shareManager.user_token ?? "")
            }
        }
    }
    
    @IBAction func verCodeBtnClick(_ sender: UIButton) {
        let phoneVC = UIStoryboard.viewController(from: .LoginSB, viewControllerClass: PhoneLoginVC.self)
        let navPhone = UINavigationController(rootViewController: phoneVC)
        self.present(navPhone, animated: true, completion: nil)
    }
    
    
    @IBAction func eyeBtnClick(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        let eyeImage = sender.isSelected ? UIImage(named: "btn_password_selected") : UIImage(named: "btn_password_nor")
        sender.setImage(eyeImage, for: .normal)
        pswTextF.isSecureTextEntry = !sender.isSelected
        // 防止明暗文切换的时候的突兀变化
        let pswText = pswTextF.text//切换的时候记录之前的密码
        pswTextF.text = ""
        pswTextF.text = pswText
    }
    
}
// 给登录的按钮扩展绑定属性
extension BCUIButton {
    var ex_loginBtnState: AnyObserver<Bool> {
        return Binder(self) { button, state in
            button.isEnabled = state
            button.backgroundColor = state ? UIColor.hex(hexString: "#2E3D4B") : UIColor.hex(hexString: "#D6DCE7")
        }.asObserver()
    }
}
